<template>
  <div class="waiter">
    <div class="waiter_title">员工管理</div>
    <div class="waiter_button">
      <el-button type="primary" id="waiter_add" size="small" @click="addWaiter">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="waiterData.list" style="width: 100%" id="waiter_table">
      <el-table-column prop="id" label="编号" min-width="78" align="center"></el-table-column>
      <el-table-column prop="username" label="姓名" min-width="144" align="center"></el-table-column>
      <el-table-column prop="telephone" label="手机号" min-width="144" align="center"></el-table-column>
      <el-table-column prop="idCard" label="身份证号" min-width="144" align="center"></el-table-column>
      <el-table-column prop="bankCard" label="银行卡号" min-width="145" align="center"></el-table-column>
      <el-table-column label="注册时间" min-width="155" align="center">
        <template slot-scope="{row}">
          <span>{{row.registerTime | dateParse}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="144" align="center"></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="deleteWaiter(row)">删除</el-button>
          <el-button type="text" size="small" @click="editWaiter(row)">编辑</el-button>
          <el-button type="text" size="small" @click="toWaiterDetails(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="waiter_page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <!-- {{form}} -->
      <el-form :model="form" ref="ruleForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.realname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.idCard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="银行卡号">
          <el-input v-model="form.bankCard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveWaiter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      page: 0,
      pageSize: 5,
      total: 10,
      dialogFormVisible: false,
      form: {
        id: "",
        username: "",
        realname: "",
        telephone: "",
        idCard: "",
        bankCard: "",
        password: ""
      },
      title: "添加员工信息"
    };
  },
  created() {
    this.loadWaiters();
  },
  computed: {
    ...mapState("waiter", ["waiterData"])
  },
  methods: {
    ...mapActions("waiter", [
      "toLoadWaiters",
      "toSaveWaiters",
      "toDeleteWaiters"
    ]),
    toWaiterDetails(row) {
      this.$router.push({ name: "waiterDetails", query: { id: row.id } });
    },
    //加载
    loadWaiters() {
      let obj = {
        page: this.page,
        pageSize: this.pageSize
      };
      this.toLoadWaiters(obj).then(res => {
        this.total = this.waiterData.total;
      });
    },
    //分页
    handleCurrentChange(val) {
      this.page = val-1;
      this.loadWaiters();
    },
    //修改
    editWaiter(row) {
      this.dialogFormVisible = true;
      this.form.id = row.id;
      this.form.username = row.username;
      this.form.realname = row.realname;
      this.form.telephone = row.telephone;
      this.form.idCard = row.idCard;
      this.form.bankCard = row.bankCard;
      this.form.password = row.password;
      this.title = "编辑用户信息";
    },
    //新增
    addWaiter() {
      this.dialogFormVisible = true;
      this.form = {};
      this.title = "添加用户信息";
    },
    //保存
    saveWaiter() {
      this.toSaveWaiters(this.form).then(response => {
        this.$message({ type: "success", message: "保存成功" });
        this.dialogFormVisible = false;
        this.loadWaiters();
      });
    },
    //删除
    deleteWaiter(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.toDeleteWaiters(row.id).then(response => {
          this.$message({ type: "success", message: "删除成功" });
          this.loadWaiters();
        });
      });
    }
  }
};
</script>
<style scope>
.waiter {
  padding: 10px;
}
.waiter_title {
  border-bottom: 1px solid #ccc;
  height: 35px;
  line-height: 35px;
}
.waiter_button {
  margin-top: 10px;
  position: relative;
}
.waiter_page {
  margin-top: 10px;
}
</style>